package Ventanas.admin;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import util.GetFrame;
import util.SocketManager;
import util.Varias;
import BaseDeDatos.ConsultasUsuario;
import ClasesBasicas.Usuario;

public class PanelInicialA extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private VentanaA ventana;

	private javax.swing.JButton bCrear;
	private javax.swing.JButton bDesconectar;
	private javax.swing.JButton bEliminar;
	private javax.swing.JButton bModificar;
	private javax.swing.JButton bSalir;
	private javax.swing.JComboBox comboBox;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JLabel lMaxConexion;
	private static JComboBox comboSockets;
	private javax.swing.JTextField tfMaxConexion;
	private JButton bNumConexiones;
	private JLabel fondo;
	private DefaultComboBoxModel dt = new DefaultComboBoxModel();
	private static DefaultComboBoxModel dtSockets = new DefaultComboBoxModel();
	private javax.swing.JComboBox comboBoxMod;
	private List<Usuario> usuarios = ConsultasUsuario.getUsuarios();
	public static List<SocketManager> sockets = new ArrayList<SocketManager>();
	private DefaultTableModel modelo;
	private javax.swing.JLabel lHay;
	private javax.swing.JLabel lSocketsConectados;
	private static javax.swing.JTextField tfSocketsConectados;

	public PanelInicialA(VentanaA v) {
		ventana = v;
		ventana.pack();
		ventana.setLocationRelativeTo(null);
		ventana.setVisible(true);
		ventana.getContentPane().add(this, "Center");
		ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		lMaxConexion = new javax.swing.JLabel();
		tfMaxConexion = new javax.swing.JTextField();
		tfMaxConexion.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		tfMaxConexion.setText(GetFrame.getVentanaA(this).getMaximoConexiones()
				+ "");
		jScrollPane1 = new javax.swing.JScrollPane();
		bDesconectar = new javax.swing.JButton();
		bCrear = new javax.swing.JButton();
		bModificar = new javax.swing.JButton();
		comboBox = new javax.swing.JComboBox();
		bEliminar = new javax.swing.JButton();
		bSalir = new javax.swing.JButton();
		bNumConexiones = new JButton();
		fondo = new JLabel();
		comboBoxMod = new javax.swing.JComboBox();
		comboSockets = new JComboBox();
		lHay = new javax.swing.JLabel();
		tfSocketsConectados = new javax.swing.JTextField();
		lSocketsConectados = new javax.swing.JLabel();

		setLayout(null);

		lMaxConexion.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		lMaxConexion.setText("Maximo numero de conexiones: ");
		lMaxConexion.setForeground(new java.awt.Color(255, 255, 255));
		add(lMaxConexion);
		lMaxConexion.setBounds(160, 45, 220, 30);
		add(tfMaxConexion);
		tfMaxConexion.setBounds(373, 45, 40, 30);

		comboSockets.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		Varias.cargarComboSockets(comboSockets, getSockets());
		comboSockets.setModel(dtSockets);
		add(comboSockets);
		comboSockets.setBounds(40, 340, 235, 30);

		bDesconectar.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		bDesconectar.setText("Desconectar");
		add(bDesconectar);
		bDesconectar.setBounds(290, 340, 110, 30);
		bDesconectar.addActionListener(this);

		bCrear.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		bCrear.setText("Crear usuario");
		add(bCrear);
		bCrear.setBounds(440, 210, 130, 30);
		bCrear.addActionListener(this);

		bModificar.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		bModificar.setText("Modificar usuario");
		add(bModificar);
		bModificar.setBounds(580, 280, 150, 30);
		bModificar.addActionListener(this);

		comboBox.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		Varias.cargarComboUsuarios(comboBox, usuarios);
		add(comboBox);
		comboBox.setBounds(440, 340, 130, 30);

		bEliminar.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		bEliminar.setText("Eliminar");
		add(bEliminar);
		bEliminar.setBounds(580, 338, 100, 30);
		bEliminar.addActionListener(this);

		bSalir.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		bSalir.setText("Salir");
		add(bSalir);
		bSalir.setBounds(640, 520, 80, 30);
		bSalir.addActionListener(this);

		bNumConexiones.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		bNumConexiones.setText("Editar");
		add(bNumConexiones);
		bNumConexiones.setBounds(420, 45, 80, 30);
		bNumConexiones.addActionListener(this);

		comboBoxMod.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		Varias.cargarComboUsuarios(comboBoxMod, usuarios);
		add(comboBoxMod);
		comboBoxMod.setBounds(440, 280, 130, 30);

		lHay.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		lHay.setText("Hay ");
		add(lHay);
		lHay.setBounds(100, 280, 30, 30);
		lHay.setForeground(new java.awt.Color(255, 255, 255));
		add(tfSocketsConectados);
		tfSocketsConectados.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		tfSocketsConectados.setBounds(130, 280, 40, 30);
		tfSocketsConectados.setEditable(false);
		tfSocketsConectados.setText(getSockets().size() + "");
		tfSocketsConectados.repaint();

		lSocketsConectados.setFont(new java.awt.Font("Comic Sans MS", 0, 14));
		lSocketsConectados.setText("sockets conectados");
		add(lSocketsConectados);
		lSocketsConectados.setBounds(180, 280, 190, 30);
		lSocketsConectados.setForeground(new java.awt.Color(255, 255, 255));

		this.setBackground(new Color(0, 102, 102));
		fondo.setIcon(new ImageIcon("src/resources/onda.png"));
		add(fondo);
		fondo.setBounds(155, 10, 516, 290);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource().equals(bEliminar)) {
			ConsultasUsuario.borrarUsuario(comboBox.getSelectedItem()
					.toString());
			comboBox.setModel(dt);
			Varias.cargarComboUsuarios(comboBox, ConsultasUsuario.getUsuarios());
			comboBox.repaint();
			this.repaint();
			comboBox.setSelectedIndex(0);
		}
		if (e.getSource().equals(bCrear)) {
			VentanaA ventana = GetFrame.getVentanaA(this);
			this.setVisible(false);
			PanelCrear crear = new PanelCrear(this);
			ventana.getContentPane().add(crear);
			crear.setVisible(true);
		}
		if (e.getSource().equals(bModificar)) {
			VentanaA ventana = GetFrame.getVentanaA(this);
			this.setVisible(false);
			PanelModificar modificar = new PanelModificar(this,
					usuarios.get(comboBoxMod.getSelectedIndex()));
			ventana.getContentPane().add(modificar);
			modificar.setVisible(true);

		}
		if (e.getSource() == (bDesconectar)) {
			VentanaA ventana = GetFrame.getVentanaA(this);
			System.out.println("Tamano primero: " + getSockets().size());
			try {
				sockets.get(comboBox.getSelectedIndex()).CerrarStreams();
				sockets.get(comboBox.getSelectedIndex()).CerrarSocket();
				getSockets().remove(comboSockets.getSelectedIndex());
				System.out.println("Tamano despues: " + getSockets().size());
				tfSocketsConectados.setText(getSockets().size() + "");
				tfSocketsConectados.repaint();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			// getSockets().remove(comboSockets.getSelectedIndex()-1);
			Varias.cargarComboSockets(comboSockets, getSockets());
			comboSockets.setModel(dtSockets);
			comboSockets.repaint();
			this.repaint();
			comboSockets.setSelectedIndex(0);

		}
		if (e.getSource().equals(bNumConexiones)) {
			ventana.setMaximoConexiones(Integer.parseInt(tfMaxConexion
					.getText()));

		}
		if (e.getSource() == (bSalir)) {
			ventana.dispose();
		}

	}

	public static List<SocketManager> getSockets() {
		return sockets;
	}

	public static void insertarSocket(SocketManager manager) {
		sockets.add(manager);
		Varias.cargarComboSockets(comboSockets, getSockets());
		comboSockets.setModel(dtSockets);
		tfSocketsConectados.setText(getSockets().size() + "");
		tfSocketsConectados.repaint();
	}

	public static void main(String[] args) {
		// Look & Feel Nimbus
		for (UIManager.LookAndFeelInfo laf : UIManager
				.getInstalledLookAndFeels()) {
			if ("Nimbus".equals(laf.getName()))
				try {
					UIManager.setLookAndFeel(laf.getClassName());
				} catch (Exception ex) {
				}
		}
		new VentanaA();
	}

}